﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using WisDom.IService;
using WisDom.Models;
using WisDom.Util;

namespace WisDom.Update.Controllers
{
    /// <summary>
    /// 包信息管理
    /// </summary>
    [Route("api/[controller]")]
    [ApiController]
    public class PackageInfoController : ControllerBase
    {
        IPackageInfoInterface packageinfoService;
        public PackageInfoController(IPackageInfoInterface packageinfoService)
        {
            this.packageinfoService = packageinfoService;
        }
        /// <summary>
        /// 获取分页数据
        /// </summary>
        /// <param name="PackageType">包类型</param>
        /// <param name="pageSize">页大小</param>
        /// <param name="pageIndex">页索引</param>
        /// <returns></returns>
        [HttpGet(Name = "GetPackageInfoPageData")]
        public async Task<IActionResult> Get([FromQuery]string PackageType, [FromQuery]int pageSize, [FromQuery]int pageIndex)
        {
            try
            {
                var pagination = new Util.Pagination { PageIndex = pageIndex, Take = pageSize };
                var lst = await packageinfoService.GetPageList(pagination, new { PackageType = PackageType }.ToJson());

                return new JsonResult(new { code = "0", msg = "获取数据成功", count = pagination.Records, data = lst });
            }
            catch (Exception ex)
            {
                return new JsonResult(new { code = "1", msg = ex.Message, count = 0 });
            }
        }


        ///// <summary>
        ///// 获取分页数据
        ///// </summary> 
        ///// <returns></returns>
        //[HttpGet]
        //public async Task<IActionResult> Get()
        //{
        //    try
        //    {
        //        var lst = await packageinfoService.GetList("");
        //        return new JsonResult(new { code = "0", msg = "获取数据成功", data = lst });
        //    }
        //    catch (Exception ex)
        //    {
        //        return new JsonResult(new { code = "1", msg = ex.Message, count = 0 });
        //    }
        //}
        /// <summary>
        /// 获取单个数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("{id}", Name = "GetPackageInfoById")]
        public async Task<IActionResult> Get(int id)
        {
            var entity = await packageinfoService.GetEntity(id.ToString());
            return new JsonResult(entity);
        }

        /// <summary>
        /// 增加数据
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<bool> Post([FromBody] PackageInfoEntity entity)
        {
            return await packageinfoService.InsertData(entity) > 0 ? true : false;
        }

        /// <summary>
        /// 更新数据
        /// </summary>
        /// <param name="id"></param>
        /// <param name="entity"></param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<bool> Put(int id, [FromBody] PackageInfoEntity entity)
        {
            return await packageinfoService.UpdateData(id.ToString(), entity) > 0 ? true : false;
        }

        /// <summary>
        /// 删除数据
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<bool> Delete(int id)
        {
            return await packageinfoService.RemoveData(id.ToString()) > 0 ? true : false;
        }        
    }
}
